/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 */
package com.jeesite.modules.vehicle.service;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.text.PinyinUtils;
import com.jeesite.modules.general.utils.CosApiUtils;
import com.jeesite.modules.vehicle.dao.CarApiDao;
import com.jeesite.modules.vehicle.entity.CarApi;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 测试数据Service
 * @author ThinkGem
 * @version 2018-04-22
 */
@Service
public class CarApiService extends CrudService<CarApiDao, CarApi> {
	DecimalFormat df = new DecimalFormat("0.00");
	/**
	 * 查询分页数据
	 * @param carApi 查询条件
	 * @return
	 */
	@Override
	public Page<CarApi> findPage(CarApi carApi) {
		return super.findPage(carApi);
	}

	/**
	 * 查询车分类
	 * @param carApi
	 * */
	public List<CarApi> findCarType(CarApi carApi){
		return dao.findCarType(carApi);
	}

	/**
	 * 查询车分类并进行字母排序
	 * @param carApi
	 * */
	public Map<String, List<CarApi>> findCarLetterType(CarApi carApi){
		List<CarApi> list =  dao.findCarType(carApi);
		for(int i=0;i<list.size();i++){
			String str = PinyinUtils.getFirstSpell(list.get(i).getTypeName()).toUpperCase();
			list.get(i).setBrand(str.substring(0,1));
		}
		// 整合
		Map<String, List<CarApi>> nameList = list.stream().collect(Collectors.groupingBy(CarApi::getBrand));
		// 排序
		Map<String, List<CarApi>> sortedMap = nameList.entrySet().stream()
				.sorted(Map.Entry.comparingByKey())
				.collect(Collectors.toMap(
						Map.Entry::getKey,
						Map.Entry::getValue,
						(oldValue, newValue) -> oldValue,
						LinkedHashMap::new
				));
		return sortedMap;
	}

	/**
	 * 查询车详情
	 * @param carId
	 * */
	public Map getCarDetail(String carId){
		Map map = new HashMap();
		List<CarApi> list = dao.getCarDetail(carId);
		if(list.size()>0){
			JSONArray array1 = new JSONArray();
			JSONArray array2 = new JSONArray();
			JSONArray array3 = new JSONArray();
			array1.add(CosApiUtils.URL+list.get(0).getCoverUrl());
			for(int i=0;i<list.size();i++){
				CarApi car = list.get(i);
				// 轮播图
				if("0".equals(car.getImageType())){
					array1.add(CosApiUtils.URL+car.getImageUrl());
				}
				// 详情图
				if("1".equals(car.getImageType())){
					array2.add(CosApiUtils.URL+car.getImageUrl());
				}
			}
			CarApi data = list.get(0);
			for(int i=0;i<9;i++){
                Map maps = new HashMap();
                if(i==0){
                    maps.put("title","名称"); maps.put("value",data.getCarTitle());
                }
                if(i==1){
                    maps.put("title","公里数");maps.put("value",data.getKilometer());
                }
                if(i==2){
                    maps.put("title","品牌");maps.put("value",data.getBrand());
                }
                if(i==3){
                    maps.put("title","过户次数");maps.put("value",data.getTransfers()+"次");
                }
                if(i==4){
                    maps.put("title","汽缸");maps.put("value",data.getCylinder());
                }
                if(i==5){
					maps.put("title","类型");maps.put("value",data.getTypeName());
				}
				if(i==6){
					maps.put("title","详细说明");maps.put("value",data.getExplain());
				}
                array3.add(maps);
            }
			data.setShowSellPrice(df.format(data.getSellPrice()/100));
			map.put("roundList", array1);
			map.put("detailList", array2);
			map.put("dataInfo", data);
			map.put("attrInfo", array3);
		}
		return map;
	}

	/**
	 * 查询车品牌
	 * @param pageSize
	 * */
	public List<CarApi> findCarBrand(Integer pageSize){
		return dao.findCarBrand(pageSize);
	}
	/**
	 * 查询车详情（移动端查看）
	 * @param carId
	 * */
	public Map getCarMobileDetail(String carId){
		Map map = new HashMap();
		List<CarApi> list = dao.getCarDetail(carId);
		if(list.size()>0){
			JSONArray array1 = new JSONArray();
			JSONArray array2 = new JSONArray();
			for(int i=0;i<list.size();i++){
				CarApi car = list.get(i);
				// 轮播图
				if("0".equals(car.getImageType())){
					array1.add(CosApiUtils.URL+car.getImageUrl());
				}
				// 详情图
				if("1".equals(car.getImageType())){
					array2.add(CosApiUtils.URL+car.getImageUrl());
				}
			}
			list.get(0).setCoverUrl(CosApiUtils.URL+list.get(0).getCoverUrl());
			map.put("carList", array1);
			map.put("detailList", array2);
			map.put("dataInfo", list.get(0));
		}
		return map;
	}
	/**
	 * 删除图片
	 * @param carApi
	 * */
	public int deleteImage(CarApi carApi){
		return dao.deleteImage(carApi);
	}

	/**
	 * 插入图片
	 * @param map
	 * */
	@Transactional
	public int insertImage(Map map){
		return dao.insertImage(map);
	}
}